import SkuEntityCollection from '@/feature/pdt/domain/SkuEntityCollection'
import SpuProperty from "@/feature/pdt/domain/SpuProperty";

export default class SpuPropertyCollection {
  constructor(values = [], isSaleProp = false, skuList = null, selectableProperties = null) {
    this.values = values
    if (isSaleProp) {
      this.skuList = new SkuEntityCollection(skuList)
    }
    this.batchOptions = []
    this.batchStock = ''
    this.isBatchAddStock = true
    this.batchPrice = ''
    this.selectableProperties = selectableProperties
  }

  get canAddProperty() {
    return !!this.selectableProperties && this.values.length < 2
  }

  propertyOptions(idx) {
    const exceptedIdList = this.values.filter((v, index) => index !== idx && v.value).map(el => el.value)
    return this.selectableProperties.filter(el => !exceptedIdList.includes(el.id))
  }

  syncPropertyIdByTitle(idx) {
    this.values[idx].syncPropertyIdByTitle(this.selectableProperties)
  }

  isPropertyEmpty(idx) {
    return this.values[idx].isEmpty()
  }

  removeProperty(idx) {
    this.values.splice(idx, 1)
  }

  addProperty() {
    this.values.push(new SpuProperty({
      deletable: true,
      selectable: true,
      uploadPicFlag: this.values.length === 0 ? 1 : 0,
      isUploadPic: false
    }, true))
  }

  get canBatchSet() {
    return this.batchStock || this.batchPrice
  }

  get isSingleSku() {
    return this.skuList.isSingleSku && this.checkedPropTitleList.length === 0
  }

  static from(values, isSaleProp = false, skuList = null, selectableProperties = null) {
    return new SpuPropertyCollection(values, isSaleProp, skuList, selectableProperties)
  }

  get skuPropWriteRates() {
    if (this.values.length) {
      return Math.round(this.values.filter(el => el.isNotEmpty).length / this.values.length * 100)
    }
    return 0
  }

  getSpuPropList() {
    return this.values.reduce((r, el) => {
      const spuProp = el.getSpuProp()
      if (spuProp) {
        r.push(spuProp)
      }
      return r
    }, [])
  }

  getSkuList() {
    return this.skuList.getValues()
  }

  onCheckedPropValueChanged() {
    this.calcBatchOptions()
    this.skuList.calcValues(this.batchOptions)
  }

  batchSet(isEditMode = false) {
    // 批量的属性值设置
    const propValueMap = this.batchOptions.reduce((r, el) => {
      r[el.id] = el.value
      return r
    }, {})
    if (this.batchStock) {
      if (!isEditMode) {
        this.skuList.batchSetField(propValueMap, 'stock', this.batchStock)
      } else {
        this.skuList.batchSetField(propValueMap, 'deltaStock', this.batchStock, this.isBatchAddStock)
      }
    }
    if (this.batchPrice) {
      this.skuList.batchSetField(propValueMap, 'price', this.batchPrice)
    }
  }

  calcBatchOptions() {
    this.batchOptions = this.values.reduce((r, p) => {
      const checkedValues = p.checkedValues
      if (checkedValues && checkedValues.length) {
        r.push({
          title: p.title,
          id: p.id,
          value: '0',
          options: checkedValues
        })
      }
      return r
    }, [])
  }

  get checkedPropTitleList() {
    return this.batchOptions.map(el => el.title)
  }

  validate() {
    this.skuList.validate()
  }

  clearValues(idx) {
    this.values[idx].clear()
  }
}
